package com.ht.medicaltreatment.viewmodel

import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.ht.medicaltreatment.bean.CheckBean
import com.ht.medicaltreatment.bean.SuccessBean
import com.ht.medicaltreatment.bean.ZTDetailBean
import com.ht.medicaltreatment.bean.ZTDetailData
import com.ht.medicaltreatment.http.api.BuildProjectConsumableAPI
import com.ht.medicaltreatment.http.api.BuildProjectConsumableUpdateAPI
import com.ht.medicaltreatment.http.api.CreateZTAPI
import com.ht.medicaltreatment.http.api.ZTDetailAPI
import com.ht.medicaltreatment.http.api.ZTDeteleAPI

class CreateZTViewModel:BaseViewModel() {

    fun checkNumber(projectId:String, sign:String, studyNo:String,onCheck:(Boolean)->Unit){
        EasyHttp.post(this)
            .api(CreateZTAPI(projectId,sign,studyNo))
            .request(object : OnHttpListener<CheckBean>{
                override fun onHttpSuccess(result: CheckBean) {
                    dismissProgress()
                    onCheck(result.data)
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                    onCheck(false)
                }
            })
    }

    fun submit(api : BuildProjectConsumableAPI,onResult:()->Unit){
        EasyHttp.post(this)
            .api(api)
            .request(object : OnHttpListener<SuccessBean> {
                override fun onHttpSuccess(result: SuccessBean) {
                    dismissProgress()
                    onResult()
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }

    fun change(api : BuildProjectConsumableUpdateAPI, onResult:()->Unit){
        EasyHttp.post(this)
            .api(api)
            .request(object : OnHttpListener<SuccessBean> {
                override fun onHttpSuccess(result: SuccessBean) {
                    dismissProgress()
                    onResult()
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }

    fun detail(id:String,OnResult:(ZTDetailData)->Unit){
        EasyHttp.post(this)
            .api(ZTDetailAPI(id))
            .request(object : OnHttpListener<ZTDetailBean> {
                override fun onHttpSuccess(result: ZTDetailBean) {
                    dismissProgress()
                    OnResult(result.data)
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }

    fun delete(id:String,OnResult:()->Unit){
        EasyHttp.post(this)
            .api(ZTDeteleAPI(id))
            .request(object : OnHttpListener<SuccessBean> {
                override fun onHttpSuccess(result: SuccessBean) {
                    dismissProgress()
                    OnResult()
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message!!)
                }
            })
    }
}